﻿/**
 * Peter
 * Created by: Peter Development Team
 *    http://peter.codeplex.com/
 * 
 * GNU General Public License version 2 (GPLv2)
 *    http://peter.codeplex.com/license
 *
 *  This code is provided on an AS IS basis, with no WARRANTIES,
 *  CONDITIONS or GUARANTEES of any kind.
 *
 **/

using System.IO;
using System.Reflection;
using Ionic.Zip;
using Peter.Common;

namespace Peter.Ruby.Tests
{
   /// <summary>
   /// A mock peter project.
   /// </summary>
   public class MockPeterProject : PeterProject
   {
      /// <summary>
      /// Represents a mocked peter project.
      /// </summary>
      /// <param name="assem">The currenly executing assembly.</param>
      /// <param name="testDir">The directory of the tests.</param>
      public MockPeterProject (Assembly assem, string testDir)
      {
         this.ProjectName = "TestProject";
         this.ProjectLocation = testDir + "\\TestProject";
         using (var s = assem.GetManifestResourceStream ("Peter.Ruby.Tests.TestProject.zip"))
         {
            if (s != null)
            {
               using (var fileStream = File.Create (testDir + "\\TestProject.zip"))
                  s.CopyTo (fileStream);

               using (var zip = new ZipFile (testDir + "\\TestProject.zip"))
                  zip.ExtractAll (testDir, ExtractExistingFileAction.OverwriteSilently);
            }
         }
      }

      /// <summary>
      /// Closes the mock project.
      /// </summary>
      /// <param name="testDir">The directory of the tests.</param>
      public void Close (string testDir)
      {
         if (File.Exists (testDir + "\\TestProject.zip"))
            File.Delete (testDir + "\\TestProject.zip");
         if (Directory.Exists (testDir + "\\TestProject"))
            Directory.Delete (testDir + "\\TestProject");
      }
   }
}
